
package gui;

import business.Categoria;
import javax.swing.JOptionPane;

public class Categoria_Principal extends javax.swing.JPanel {

    private static Categoria_Principal instance;

    void setjTextFieldCategoriaPrincipalNomeEnabled(boolean edit) {
        jTextFieldCategoriaPrincipalNome.setEnabled(edit);
    }

    void setjTextFieldCategoriaPrincipalTarifa(String tarifa) {
        jTextFieldCategoriaPrincipalTarifa.setText(tarifa);
    }
    
    public enum StatusCategoriaPrincipal{
        scInserindo,
        scPesquisado
    }
    
    private StatusCategoriaPrincipal sStatus;
    
    public Categoria_Principal() {
        sStatus = StatusCategoriaPrincipal.scInserindo;
        initComponents();
    }

    public static Categoria_Principal getInstance(){
        if (instance == null)
            instance = new Categoria_Principal();
        return instance;
    }
    
    public StatusCategoriaPrincipal getsStatus() {
        return sStatus;
    }

    public void setsStatus(StatusCategoriaPrincipal sStatus) {
        this.sStatus = sStatus;
    }

    public void setjTextFieldCategoriaPrincipalNome(String nome) {
        jTextFieldCategoriaPrincipalNome.setText(nome);
    }
    
    
    private void limpaTela(){
        this.jTextFieldCategoriaPrincipalNome.setText("");
        this.jTextFieldCategoriaPrincipalTarifa.setText("");        
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabelCategoriaPrincipalNome = new javax.swing.JLabel();
        jLabelCategoriaPrincipalTarifa = new javax.swing.JLabel();
        jTextFieldCategoriaPrincipalTarifa = new javax.swing.JTextField();
        jTextFieldCategoriaPrincipalNome = new javax.swing.JTextField();
        jButtonCategoriaPrincipalSalvar = new javax.swing.JButton();
        jButtonCategoriaPrincipalEditar = new javax.swing.JButton();
        jButtonCategoriaPrincipalPesquisar = new javax.swing.JButton();
        jButtonCategoriaPrincipalDeletar = new javax.swing.JButton();
        jButtonCategoriaPrincipalNovo = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("gui/Bundle"); // NOI18N
        jLabel2.setText(bundle.getString("Categoria_Principal.jLabel2.text")); // NOI18N

        jLabelCategoriaPrincipalNome.setText(bundle.getString("Categoria_Principal.jLabelCategoriaPrincipalNome.text")); // NOI18N

        jLabelCategoriaPrincipalTarifa.setText(bundle.getString("Categoria_Principal.jLabelCategoriaPrincipalTarifa.text")); // NOI18N

        jTextFieldCategoriaPrincipalTarifa.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldCategoriaPrincipalTarifaFocusLost(evt);
            }
        });

        jButtonCategoriaPrincipalSalvar.setText(bundle.getString("Categoria_Principal.jButtonCategoriaPrincipalSalvar.text")); // NOI18N
        jButtonCategoriaPrincipalSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCategoriaPrincipalSalvarActionPerformed(evt);
            }
        });

        jButtonCategoriaPrincipalEditar.setText(bundle.getString("Categoria_Principal.jButtonCategoriaPrincipalEditar.text")); // NOI18N
        jButtonCategoriaPrincipalEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCategoriaPrincipalEditarActionPerformed(evt);
            }
        });

        jButtonCategoriaPrincipalPesquisar.setText(bundle.getString("Categoria_Principal.jButtonCategoriaPrincipalPesquisar.text")); // NOI18N
        jButtonCategoriaPrincipalPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCategoriaPrincipalPesquisarActionPerformed(evt);
            }
        });

        jButtonCategoriaPrincipalDeletar.setText(bundle.getString("Categoria_Principal.jButtonCategoriaPrincipalDeletar.text")); // NOI18N
        jButtonCategoriaPrincipalDeletar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCategoriaPrincipalDeletarActionPerformed(evt);
            }
        });

        jButtonCategoriaPrincipalNovo.setText(bundle.getString("Categoria_Principal.jButtonCategoriaPrincipalNovo.text")); // NOI18N
        jButtonCategoriaPrincipalNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCategoriaPrincipalNovoActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText(bundle.getString("Categoria_Principal.jLabel1.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButtonCategoriaPrincipalDeletar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                            .addComponent(jButtonCategoriaPrincipalPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonCategoriaPrincipalEditar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(30, 30, 30))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonCategoriaPrincipalNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonCategoriaPrincipalSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelCategoriaPrincipalTarifa)
                            .addComponent(jLabelCategoriaPrincipalNome))
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldCategoriaPrincipalNome, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                            .addComponent(jTextFieldCategoriaPrincipalTarifa))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCategoriaPrincipalNome)
                    .addComponent(jTextFieldCategoriaPrincipalNome, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonCategoriaPrincipalNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelCategoriaPrincipalTarifa)
                            .addComponent(jTextFieldCategoriaPrincipalTarifa, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCategoriaPrincipalSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCategoriaPrincipalEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCategoriaPrincipalPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCategoriaPrincipalDeletar, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCategoriaPrincipalEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCategoriaPrincipalEditarActionPerformed
         if (sStatus != StatusCategoriaPrincipal.scPesquisado)
           JOptionPane.showMessageDialog(this,"Pesquise uma categoria para editá-la.");
         else
         {   
             Categoria.alterarCategoria(jTextFieldCategoriaPrincipalNome.getText(),jTextFieldCategoriaPrincipalTarifa.getText());  
             jTextFieldCategoriaPrincipalNome.setEnabled(true);
             sStatus = StatusCategoriaPrincipal.scInserindo;
             limpaTela();
         }
    }//GEN-LAST:event_jButtonCategoriaPrincipalEditarActionPerformed

    private void jButtonCategoriaPrincipalPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCategoriaPrincipalPesquisarActionPerformed
        Categoria_Pesquisar frame = new Categoria_Pesquisar();
        frame.setVisible(true);
    }//GEN-LAST:event_jButtonCategoriaPrincipalPesquisarActionPerformed

    private void jButtonCategoriaPrincipalSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCategoriaPrincipalSalvarActionPerformed
        if (sStatus == StatusCategoriaPrincipal.scInserindo){
             if (jTextFieldCategoriaPrincipalNome.getText().isEmpty() ||jTextFieldCategoriaPrincipalTarifa.getText().isEmpty() ){
                JOptionPane.showMessageDialog(this,"Existem campos em branco!");
                return;
             }
             
            if (Categoria.categoriaExiste(jTextFieldCategoriaPrincipalNome.getText())){
                JOptionPane.showMessageDialog(this,"Categoria já existente!"); 
                return;
            }

            Categoria.insertCategoria(jTextFieldCategoriaPrincipalNome.getText(), jTextFieldCategoriaPrincipalTarifa.getText());
            limpaTela();
        }
        else 
            JOptionPane.showMessageDialog(this, "Para inserir uma nova categoria, clique em novo.");
    }//GEN-LAST:event_jButtonCategoriaPrincipalSalvarActionPerformed

    private void jButtonCategoriaPrincipalNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCategoriaPrincipalNovoActionPerformed
        jTextFieldCategoriaPrincipalNome.setText("");
        jTextFieldCategoriaPrincipalTarifa.setText("");
        jTextFieldCategoriaPrincipalNome.setEnabled(true);
        sStatus = StatusCategoriaPrincipal.scInserindo;
    }//GEN-LAST:event_jButtonCategoriaPrincipalNovoActionPerformed

    private void jButtonCategoriaPrincipalDeletarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCategoriaPrincipalDeletarActionPerformed
        if (sStatus != StatusCategoriaPrincipal.scPesquisado)
           JOptionPane.showMessageDialog(this,"Pesquise uma categoria para deletá-la.");
        else{
            Categoria.deleteCategoriaPeloNome(jTextFieldCategoriaPrincipalNome.getText());
            limpaTela();
            sStatus=StatusCategoriaPrincipal.scInserindo;
            jTextFieldCategoriaPrincipalNome.setEnabled(true);
        }
    }//GEN-LAST:event_jButtonCategoriaPrincipalDeletarActionPerformed

    private void jTextFieldCategoriaPrincipalTarifaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldCategoriaPrincipalTarifaFocusLost
        String tarifa = jTextFieldCategoriaPrincipalTarifa.getText();
        for (int i = 0; i < tarifa.length(); i++){
            tarifa = tarifa.replaceFirst(".","");
            if (!Character.isDigit(tarifa.charAt(i))){
                JOptionPane.showMessageDialog(this, "Valor de tarifa inválido!");
                jTextFieldCategoriaPrincipalTarifa.requestFocus();
                jTextFieldCategoriaPrincipalTarifa.selectAll();
                return;
            }
        }

    }//GEN-LAST:event_jTextFieldCategoriaPrincipalTarifaFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCategoriaPrincipalDeletar;
    private javax.swing.JButton jButtonCategoriaPrincipalEditar;
    private javax.swing.JButton jButtonCategoriaPrincipalNovo;
    private javax.swing.JButton jButtonCategoriaPrincipalPesquisar;
    private javax.swing.JButton jButtonCategoriaPrincipalSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabelCategoriaPrincipalNome;
    private javax.swing.JLabel jLabelCategoriaPrincipalTarifa;
    private javax.swing.JTextField jTextFieldCategoriaPrincipalNome;
    private javax.swing.JTextField jTextFieldCategoriaPrincipalTarifa;
    // End of variables declaration//GEN-END:variables
}
